package com.lgc.javathread.threadWaitTest.waitNotify;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Object a = new Object();
        Object b = new Object();
        Object c = new Object();

        Thread t1 = new Thread(new PrintLetter("A", a, b));
        Thread t2 = new Thread(new PrintLetter("B", b, c));
        Thread t3 = new Thread(new PrintLetter("C", c, a));

        t1.start();
        Thread.sleep(1000);
        t2.start();
        Thread.sleep(1000);
        t3.start();
    }
}

class PrintLetter implements Runnable {

    private String letter;
    private Object curr;
    private Object next;

    public PrintLetter(String letter, Object curr, Object next) {
        this.letter = letter;
        this.curr = curr;
        this.next = next;
    }

    @Override
    public void run() {

        for (int i = 0; i < 10; i++) {

            System.out.println(letter);

            synchronized (curr) {
                synchronized (next) {
                    next.notify();
                }

                try {
                    curr.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
